package utils;

import java.util.*;
import java.io.*;

public class StartPoint
{
    private Properties		 p = null;
    private final String     fileName; 
    private FileInputStream  myin;
    private FileOutputStream myout;
    
   
    public StartPoint() 
    {
		this.fileName = this.getClass().getClassLoader().getResource(".")
				.getPath() + "config.properties";
		p = new Properties();

		File file = new File(fileName);
		try {
			if (!file.exists())
				file.createNewFile();

			myin = new FileInputStream(file);
			p.load(myin);
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}
    

    public boolean save(String key,String value)
    {
    	p.setProperty(key, value);
    	try 
    	{
    		File file= new File(fileName);
    		myout = new FileOutputStream(file);
			p.store(myout, fileName);
			myout.flush();
		}
    	catch (IOException e) 
		{
			e.printStackTrace();
			return false;
		}
    	
    	return true;
    }
    

    public String get(String key)
    {
    	String value = null;

    	try 
    	{
    		 value = p.getProperty(key);
		}
    	catch (Exception e) 
		{
			e.printStackTrace();
			return null;
		}
    	
    	return value;
    }
   
   public static void main(String [] args)
   {
/*	   StartPoint.getInstance().save("text", "123456");
	   StartPoint.getInstance().save("text1", "12345");
	   StartPoint.getInstance().save("text2", "12346");
	   */
//	   StartPoint sp = new StartPoint("thread1.ini");
	  // sp.save("id", "12345");
//	   System.out.println(sp.get("id"));
	   
	   StartPoint sp = new StartPoint();
	   sp.save("friend_start", "123");
	   System.out.println(sp.get("friend_start"));

   }
    
}
